package com.example.jpa.enums;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/12 18:57
 */
public class IntegerCodeToEnumConverterFactory implements ConverterFactory<Integer, BaseEnum>
{

    private static final Map<Class, Converter> CONVERTERS = new HashMap<>();

    @Override
    public <T extends BaseEnum> Converter<Integer, T> getConverter(Class<T> targetType)
    {
        Converter<Integer, T> converter = CONVERTERS.get(targetType);
        if (converter == null)
        {
            converter = new IntegerToEnumConverter<>(targetType);
            CONVERTERS.put(targetType, converter);
        }
        return converter;

    }
}
